package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"sale_system/order_srv/proto"
)

var client proto.OrderClient
var conn *grpc.ClientConn

func Init() {
	var err error

	conn, err = grpc.Dial("192.168.56.103:8899", grpc.WithInsecure())
	if err != nil {
		panic("failed to connect, " + err.Error())
	}

	client = proto.NewOrderClient(conn)
}

// 测试购物车接口

func TestCreateCartItem(UserId int, GoodsId int, GoodsNum int) {

	request := proto.CartItemRequest{
		GoodsId: int32(GoodsId),
		UserId:  int32(UserId),
		Nums:    int32(GoodsNum),
	}
	rsp, err := client.CreateCartItem(context.Background(), &request)
	if err != nil {
		fmt.Println("CreateCartItem err:", err)
	}

	fmt.Println("CreateCartItem rsp:", rsp)
}
func TestUpdateCartItem(UserId int, GoodsId int, GoodsNum int) {

	request := proto.CartItemRequest{
		GoodsId: int32(GoodsId),
		UserId:  int32(UserId),
		Nums:    int32(GoodsNum),
		Checked: true,
	}
	_, err := client.UpdateCartItem(context.Background(), &request)
	if err != nil {
		fmt.Println("CreateCartItem err:", err)
	}
}
func TestDeleteCartItem() {

	request := proto.CartItemRequest{
		UserId:  999,
		GoodsId: 422,
	}
	_, err := client.DeleteCartItem(context.Background(), &request)
	if err != nil {
		fmt.Println("DeleteCartItem err:", err)
	}

}
func TestGetCartItemList(UserId int) {

	request := proto.UserInfo{
		Id: int32(UserId),
	}

	rsp, err := client.CartItemList(context.Background(), &request)
	if err != nil {
		fmt.Println("GetCartItemList err:", err)
	}
	fmt.Println("GetCartItemList rsp:", rsp)
}

// 测试订单接口
func TestCreateOrder() {
	request := proto.OrderRequest{
		UserId:  18,
		Address: "广东省珠海市",
		Name:    "陈志鸿",
		Mobile:  "13147065653",
		Post:    "default_post",
	}
	rsp, err := client.CreateOrder(context.Background(), &request)
	if err != nil {
		fmt.Println("CreateOrder err:", err)
	}
	fmt.Println("CreateOrder rsp:", rsp)
}

func TestOrderList(UserId int) {
	request := proto.OrderFilterRequest{
		UserId: int32(UserId),
	}
	rsp, err := client.OrderList(context.Background(), &request)
	if err != nil {
		fmt.Println("OrderList err:", err)
	}
	fmt.Println("OrderList rsp:", rsp)
}

func TestOrderDetail() {
	request := proto.OrderRequest{
		Id:     2,
		UserId: 18,
	}
	rsp, err := client.OrderDetail(context.Background(), &request)
	if err != nil {
		fmt.Println("OrderDetail err:", err)
	}
	fmt.Println("OrderDetail rsp:", rsp)
}

func TestUpdateStatus() {
	request := proto.OrderStatus{
		OrderSn: "202561111291851",
		Status:  "测试",
	}
	_, _ = client.UpdateOrderStatus(context.Background(), &request)

}
func main() {

	Init()
	defer conn.Close()
	TestUpdateStatus()
}
